package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.*;
import com.woniuxy.controller.info.GetAllGroupNameInfo;
import com.woniuxy.controller.info.GroupCatInfo;
import com.woniuxy.controller.info.SaveGroupInfo;
import com.woniuxy.dao.*;
import com.woniuxy.exception.*;
import com.woniuxy.model.*;
import com.woniuxy.service.PawnAttrGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@Service
public class PawnAttrGroupServiceImpl extends ServiceImpl<PawnAttrGroupDao, PawnAttrGroup> implements PawnAttrGroupService {

    /**
     * 属性组与分类关联表
     */
    @Resource
    private PawnAttrGroupCatRelDao pagcrd;
    /**
     * 分类表
     */
    @Resource
    private PawnProductCatDao ppcd;
    /**
     * 属性配置表
     */
    @Resource
    private PawnAttrConfDao pacd;

    @Resource
    private PawnGoodsAttrRelDao pgard;
    /**
     * 获取所有属性组信息
     * @return data返回GroupCatInfo
     */
    @Override
    public Result getAllGroupCat(GroupForm gf) {
        Page<PawnAttrGroup> groupForm = baseMapper.selectPage(gf, null);
        Page<GroupCatInfo> groupCatInfoPage = new Page<>();
        List<PawnAttrGroup> records = groupForm.getRecords();
        List<GroupCatInfo> groupCatInfos = new ArrayList<>();
        for(PawnAttrGroup p :records){
            //查询对应的大类
            PawnAttrGroupCatRel group_code = pagcrd.selectOne(new QueryWrapper<PawnAttrGroupCatRel>().eq("group_code", p.getGroupCode()));

            //查询所属的大类名
            PawnProductCat cat_code = ppcd.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_code", group_code.getCatCode()));

            GroupCatInfo groupCatInfo = new GroupCatInfo();
            groupCatInfo.setCatName(cat_code.getCatName());
            groupCatInfo.setGroupStat(p.getGroupStat());
            groupCatInfo.setGroupName(p.getGroupName());
            groupCatInfo.setGroupCode(p.getGroupCode());
            //根据group_code查询数量
            Integer num = pacd.selectCount(new QueryWrapper<PawnAttrConf>().eq("group_code", p.getGroupCode()));
            groupCatInfo.setNum(num);
            groupCatInfos.add(groupCatInfo);
        }
        groupCatInfoPage.setRecords(groupCatInfos);
        groupCatInfoPage.setTotal(groupForm.getTotal());
        groupCatInfoPage.setCurrent(groupForm.getCurrent());
        groupCatInfoPage.setSize(groupForm.getSize());
        return Result.success(200,"查询成功",groupCatInfoPage);
    }

    /**
     * 根据group_name模糊查询
     * @param gf 参数为str
     * @return data返回GroupCatInfo
     */
    @Override
    public Result getGroupCatByLike(GroupForm gf) throws DdException {
        QueryWrapper<PawnAttrGroup> wrapper = new QueryWrapper<PawnAttrGroup>().like("group_name", gf.getStr());
        Page<PawnAttrGroup> groupForm = baseMapper.selectPage(gf, wrapper);
        List<PawnAttrGroup> records = groupForm.getRecords();
        if(ObjectUtils.isEmpty(records)){
            throw new GroupNotExsitsException(50004,"该类型不存在");
        }else{
            Page<GroupCatInfo> groupCatInfoPage = new Page<>();
            List<GroupCatInfo> groupCatInfos = new ArrayList<>();
            for(PawnAttrGroup p :records){
                GroupCatInfo groupCatInfo = new GroupCatInfo();
                //查询对应的大类
                PawnAttrGroupCatRel group_code = pagcrd.selectOne(new QueryWrapper<PawnAttrGroupCatRel>().eq("group_code", p.getGroupCode()));

                PawnProductCat cat_code = ppcd.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_code", group_code.getCatCode()));

                groupCatInfo.setGroupCode(p.getGroupCode());
                groupCatInfo.setGroupStat(p.getGroupStat());
                groupCatInfo.setGroupName(p.getGroupName());
                groupCatInfo.setCatName(cat_code.getCatName());
                //根据group_code查询数量
                Integer num = pacd.selectCount(new QueryWrapper<PawnAttrConf>().eq("group_code", p.getGroupCode()));
                groupCatInfo.setNum(num);
                groupCatInfos.add(groupCatInfo);
            }
            groupCatInfoPage.setRecords(groupCatInfos);
            groupCatInfoPage.setTotal(groupForm.getTotal());
            groupCatInfoPage.setCurrent(groupForm.getCurrent());
            groupCatInfoPage.setSize(groupForm.getSize());
            return Result.success(200,"查询成功",groupCatInfoPage);
        }
    }

    /**
     * 根据group_code删除
     * @param dgf group_code集合
     * @return data为null
     */
    @Override
    @Transactional
    public Result deleteGroupByGid(DeleteGroupFrom dgf) throws DdException{
        List<GroupCatInfo> isChecked = dgf.getIsChecked();
        if(ObjectUtils.isEmpty(isChecked)){
            throw new CheckedIsNullException(50009,"请勾选后删除");
        }else{
            for(GroupCatInfo g : isChecked){
                List<PawnAttrConf> group_code = pacd.selectList(new QueryWrapper<PawnAttrConf>().eq("group_code", g.getGroupCode()));
                if(!ObjectUtils.isEmpty(group_code)){
                    for(PawnAttrConf s : group_code){
                        List<PawnGoodsAttrRel> attr_code = pgard.selectList(new QueryWrapper<PawnGoodsAttrRel>().eq("attr_code", s.getAttrCode()));
                        if(!ObjectUtils.isEmpty(attr_code)){
                            throw new ProductCatNameExsitsException(50002,"该属性组下已有商品存在");
                        }
                    }
                }
            }
            for(GroupCatInfo g : isChecked){
                //先删group表
                baseMapper.deleteById(g.getGroupCode());
                //再删group_cat表
                pagcrd.delete(new QueryWrapper<PawnAttrGroupCatRel>().eq("group_code",g.getGroupCode()));
                //删conf表
                List<PawnAttrConf> group_code = pacd.selectList(new QueryWrapper<PawnAttrConf>().eq("group_code", g.getGroupCode()));
                for(PawnAttrConf gac : group_code){
                    pgard.delete(new QueryWrapper<PawnGoodsAttrRel>().eq("attr_code",gac.getAttrCode()));
                }
                pacd.delete(new QueryWrapper<PawnAttrConf>().eq("group_code",g.getGroupCode()));
            }
            return Result.success(200,"删除成功",null);
        }
    }

    /**
     * 根据groupCode查询
     * @param sgf groupCode
     * @return SaveGroupInfo
     */
    @Override
    public Result getGroupByCode(SaveGroupForm sgf) {
        //获取类型名称和状态
        PawnAttrGroup pawnAttrGroup = baseMapper.selectById(sgf.getGroupCode());
        //查询待选商品大类和适用商品大类
        //大类id
        PawnAttrGroupCatRel cat_code = pagcrd.selectOne(new QueryWrapper<PawnAttrGroupCatRel>().eq("group_code", sgf.getGroupCode()));
        //适用商品大类
        PawnProductCat cat = ppcd.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_code", cat_code.getCatCode()));
        //所有商品大类
        QueryWrapper<PawnProductCat> wrapper = new QueryWrapper<>();
        wrapper.eq("p_cat_code", "0");
        List<PawnProductCat> p_cat_code = ppcd.selectList(wrapper);
        List<String> strings = new ArrayList<>();
        for(PawnProductCat p : p_cat_code){
            strings.add(p.getCatName());
        }
        SaveGroupInfo saveGroupInfo = new SaveGroupInfo();
        saveGroupInfo.setGroupName(pawnAttrGroup.getGroupName());
        saveGroupInfo.setGroupStat(pawnAttrGroup.getGroupStat());
        saveGroupInfo.setPCatName(cat.getCatName());
        saveGroupInfo.setPCatNames(strings);
        return Result.success(200,"查询成功",saveGroupInfo);
    }

    /**
     * 修改属性
     * @param sgf 商品属性组信息
     * @return data为null
     */
    @Override
    @Transactional
    public Result saveGroupByCode(SaveGroupForm sgf) throws DdException{
        PawnAttrGroup name = baseMapper.selectOne(new QueryWrapper<PawnAttrGroup>().eq("group_name", sgf.getGroupName()));
        if(!ObjectUtils.isEmpty(name)&&name.getGroupCode()!=sgf.getGroupCode()){
            throw new ProductCatErrorException(50007,"该属性组已存在");
        }else{
            List<String> pCatName = sgf.getPcatName();
            if(pCatName.size()==0){
                throw new ProductCatNotExsitsException(50006,"未选择适用大类");
            }else if(pCatName.size()>1){
                throw new ProductCatErrorException(50007,"只能适用一个大类");
            }else{
                PawnAttrGroup pawnAttrGroup = NewCopyBean.copyOne(sgf, PawnAttrGroup.class);
                //修改属性组表
                baseMapper.updateById(pawnAttrGroup);
                //修改属性组分类关联表
                for(String s : pCatName){
                    PawnProductCat cat = ppcd.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_name", s));
                    PawnAttrGroupCatRel pawnAttrGroupCatRel = new PawnAttrGroupCatRel();
                    pawnAttrGroupCatRel.setGroupCode(sgf.getGroupCode());
                    pawnAttrGroupCatRel.setCatCode(cat.getCatCode());
                }
                return Result.success(200,"修改成功",null);
            }
        }

    }

    /**
     * 查询所有大类名
     * @return
     */
    @Override
    public Result getAllDProduct() {
        List<PawnProductCat> p_cat_code = ppcd.selectList(new QueryWrapper<PawnProductCat>().eq("p_cat_code", "0"));
        List<String> strings = new ArrayList<>();
        for(PawnProductCat p:p_cat_code ){
            strings.add(p.getCatName());
        }
        SaveGroupInfo saveGroupInfo = new SaveGroupInfo();
        saveGroupInfo.setPCatNames(strings);
        return Result.success(200,"请求成功",saveGroupInfo);
    }

    /**
     * 新增商品属性分组
     * @param angf
     * @return
     */
    @Override
    @Transactional
    public Result addNewGroup(AddNewGroupForm angf) throws DdException {
        PawnAttrGroup name = baseMapper.selectOne(new QueryWrapper<PawnAttrGroup>().eq("group_name", angf.getGroupName()));
        if(!ObjectUtils.isEmpty(name)){
            throw new ProductCatErrorException(50007,"该属性组已存在");
        }else{
            List<String> pcatName = angf.getPcatName();
            if(pcatName.size()==0){
                throw new ProductCatNotExsitsException(50006,"未选择适用大类");
            }else if(pcatName.size()>1){
                throw new ProductCatErrorException(50007,"只能适用一个大类");
            }else{
                //新增属性组表
                PawnAttrGroup pawnAttrGroup = NewCopyBean.copyOne(angf, PawnAttrGroup.class);
                baseMapper.insert(pawnAttrGroup);
                //新增属性组分类关联表
                for(String s : pcatName){
                    PawnProductCat cat = ppcd.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_name", s));
                    PawnAttrGroup group = baseMapper.selectOne(new QueryWrapper<PawnAttrGroup>().eq("group_name", angf.getGroupName()));
                    PawnAttrGroupCatRel pawnAttrGroupCatRel = new PawnAttrGroupCatRel();
                    pawnAttrGroupCatRel.setCatCode(cat.getCatCode());
                    pawnAttrGroupCatRel.setGroupCode(group.getGroupCode());
                    pagcrd.insert(pawnAttrGroupCatRel);
                }
                return Result.success(200,"新增成功",null);
            }
        }
    }

    /**
     * 根据大类id获取所有属性组名
     * @param gcbccf groupCode
     * @return
     */
    @Override
    public Result getAllGroupName(GetConfByCatCodeForm gcbccf) {
        QueryWrapper<PawnAttrGroupCatRel> wrapper = new QueryWrapper<>();
        wrapper.eq("cat_code", gcbccf.getCatCode());
        //多个属性组
        List<PawnAttrGroupCatRel> catRels = pagcrd.selectList(wrapper);
        List<GetAllGroupNameInfo> infos = new ArrayList<>();
        for(PawnAttrGroupCatRel p : catRels){
            GetAllGroupNameInfo getAllGroupNameInfo = new GetAllGroupNameInfo();
            PawnAttrGroup pawnAttrGroup = baseMapper.selectById(p.getGroupCode());
            if(pawnAttrGroup.getGroupStat().equals("启用")){
                getAllGroupNameInfo.setGroupName(pawnAttrGroup.getGroupName());
                infos.add(getAllGroupNameInfo);
            }
        }
        return Result.success(200,"请求成功",infos);
    }
}
